package org.example.servlet.staff;

import com.google.gson.Gson;
import org.example.dao.StaffDao;
import org.example.entity.Staff;
import org.example.util.RequestUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


@WebServlet("/add_staff")
    public class AddStaffServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

    String json = RequestUtil.getRequestParameterStr(req);
    Gson gson = new Gson();
    Staff staff = gson.fromJson(json, Staff.class);

    int row = StaffDao.addStaff(staff);
    if (row > 0){
        System.out.println("添加成功");
    }else {
        System.out.println("添加失败");
    }
    PrintWriter writer = resp.getWriter();
    writer.write(json);
    // 刷新页面
    writer.flush();
}
}
